package com.android.place360.current_location;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.android.place360.R;

public class MyLocationListener implements LocationListener {

	private Context context;
	private TextView txtLocation;

	public static Location myCurrentLocation;
	public static String myCurrentAddress;

	private static MyLocationListener instance;

	// Singleton pattern
	public static MyLocationListener getInstance(Context context,
			TextView textView) {
		if (instance != null)
			return instance;
		return new MyLocationListener(context, textView);
	}

	private MyLocationListener(Context context, TextView textView) {
		super();
		this.context = context;
		txtLocation = textView;
	}

	
	
	public TextView getTxtLocation() {
		return txtLocation;
	}

	public void setTxtLocation(TextView txtLocation) {
		this.txtLocation = txtLocation;
	}

	@Override
	public void onLocationChanged(Location loc) {
		myCurrentLocation = loc;
		myCurrentAddress = getAddress(myCurrentLocation);

		updateTextView();
//		String text = "Your location coordinates : " + loc.getLatitude() + ","
//				+ loc.getLongitude();
//		Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onProviderDisabled(String provider) {
		Toast.makeText(context, "Location provider is disabled",
				Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onProviderEnabled(String provider) {
		Toast.makeText(context, "Location provider is enabled",
				Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
	}

	public String getAddress(Location loc) {
		if(loc==null) return null;
		try {
			Geocoder gc = new Geocoder(context, new Locale(
					com.android.place360.appsettings.SharePreference_Setting
							.getLanguage(context)));
			List<Address> addresses = gc.getFromLocation(loc.getLatitude(), loc
					.getLongitude(), 1);
			StringBuilder sb = new StringBuilder();
			if (addresses.size() > 0) {
				Address address = addresses.get(0);
				for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
					sb.append(address.getAddressLine(i)).append(", ");

				sb.append(address.getCountryName());
			}

			return sb.toString();
		} catch (IOException ex) {
			Log.i("Error", "Error " + ex.getMessage());
			return context.getString(R.string.unknow_address_alert);
		}
	}

	public void setLocation(Location loc) {
		myCurrentLocation = loc;
		myCurrentAddress = getAddress(myCurrentLocation);
		if(myCurrentLocation == null || myCurrentAddress ==null)
			txtLocation.setText(context.getString(R.string.unknow_location_alert));
		else
			updateTextView();
		
	}

	public void setLocation(Address add) {

		try {

			Location loc = new Location(MyLocationManager.provider);
        	loc.setLatitude(add.getLatitude());
        	loc.setLongitude(add.getLongitude());
        	myCurrentLocation = loc;
        	
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < add.getMaxAddressLineIndex(); i++)
				sb.append(add.getAddressLine(i)).append(", ");
			sb.append(add.getCountryName());
			
			myCurrentAddress = sb.toString();
			updateTextView();
		} catch (Exception ex) {
			Log.i("Error", "Error " + ex.getMessage());
		}

	}

	private void updateTextView(){
		String near ="";
		try{
			near = context.getString(R.string.current_address_near) + " ";
		}catch (Exception e) {
			e.printStackTrace();
		}
		txtLocation.setText(near + myCurrentAddress);
	}

	public void updateLocationLanguage(){
		myCurrentAddress = getAddress(myCurrentLocation);
		if(myCurrentAddress == null)
			txtLocation.setText(context.getString(R.string.unknow_location_alert));
		else
			updateTextView();
	}
}
